package quick.portal.activity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.Request;




import quick.communication.AsyncHttpResponseHandler;
import quick.portal.data.APIResult;
import quick.portal.data.EmailInfo;
import quick.portal.data.NoticeInfo;
import quick.portal.data.SystemMessage;
import quick.portal.service.BaseService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class MonitingService extends Service {

	private static int delay = 1000*60;
	
	private Timer timer = null;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		
		this.timer = new Timer();		
		this.timer.schedule(new TimerTask() {  
            @Override  
            public void run() {  
            	getLastTodo();
            	getLastNotice();
            	getLastEmail();
            }  
        }, delay,delay);  
	}
	
	private void getLastTodo()
	{
		new BaseService().GetObject("LastSystemMessage", new AsyncHttpResponseHandler(){
			@Override
            public void onSuccess(String body) {     		
        		try
        		{
        			APIResult result = JSON.parseObject(body, APIResult.class);
        			
        			if(result.getReturnCode()==0)
    	        	{
        				SystemMessage todo = JSON.parseObject(result.getBody(), SystemMessage.class);
        				new QuickNotification().showTodoNotification(todo.getName(), todo.getSendInfo());
    	        	}
        		}
	        	catch(Exception ex)
	        	{
	        		//
	        	}

        	}
        	
        	@Override
            public void onFailure(Request request, IOException ex) {
        		//
            }
		});
	}
	
	private void getLastNotice()
	{
		new BaseService().GetObject("LastNoticeInfo", new AsyncHttpResponseHandler(){
			@Override
            public void onSuccess(String body) {     		
        		try
        		{
        			APIResult result = JSON.parseObject(body, APIResult.class);
        			
        			if(result.getReturnCode()==0)
    	        	{
        				NoticeInfo notice = JSON.parseObject(result.getBody(), NoticeInfo.class);
        				new QuickNotification().showNoticeNotification(notice.getName(), notice.getApproveUserName() + " " + notice.getApproveTime());
    	        	}
        		}
	        	catch(Exception ex)
	        	{
	        		//
	        	}

        	}
        	
        	@Override
            public void onFailure(Request request, IOException ex) {
        		//
            }
		});
	}
	
	private void getLastEmail()
	{
		new BaseService().GetObject("LastMyReceiveEmail", new AsyncHttpResponseHandler(){
			@Override
            public void onSuccess(String body) {     		
        		try
        		{
        			APIResult result = JSON.parseObject(body, APIResult.class);
        			
        			if(result.getReturnCode()==0)
    	        	{
        				EmailInfo email = JSON.parseObject(result.getBody(), EmailInfo.class);
        				new QuickNotification().showEmailNotification(email.getTitle(), email.getFromUserName() + " " + email.getReceiveTime());
    	        	}
        		}
	        	catch(Exception ex)
	        	{
	        		//
	        	}

        	}
        	
        	@Override
            public void onFailure(Request request, IOException ex) {
        		//
            }
		});
	}
}
